options compress=YES;
libname manasi ''; 

* CREATE VARIABLES FOR MEDICAL REVIEW ANALYSIS;
	
data manasi.kids_ssr_full_prep2;
	set manasi.kids_ssr_full_prep1;
	* Keep only kids, not their future spouses;
	if hun=pan;

* CREATE MACROS FOR ARRAY AND DROP ;
	%macro array1d(var);	
	array &var.1(1:360) &var._1983_1-&var._1983_12
						&var._1984_1-&var._1984_12
						&var._1985_1-&var._1985_12
						&var._1986_1-&var._1986_12
						&var._1987_1-&var._1987_12
						&var._1988_1-&var._1988_12
						&var._1989_1-&var._1989_12
						&var._1990_1-&var._1990_12
						&var._1991_1-&var._1991_12
						&var._1992_1-&var._1992_12
						&var._1993_1-&var._1993_12
						&var._1994_1-&var._1994_12
						&var._1995_1-&var._1995_12
						&var._1996_1-&var._1996_12
						&var._1997_1-&var._1997_12
						&var._1998_1-&var._1998_12
						&var._1999_1-&var._1999_12
						&var._2000_1-&var._2000_12
						&var._2001_1-&var._2001_12
						&var._2002_1-&var._2002_12
						&var._2003_1-&var._2003_12
						&var._2004_1-&var._2004_12
						&var._2005_1-&var._2005_12
						&var._2006_1-&var._2006_12
						&var._2007_1-&var._2007_12
						&var._2008_1-&var._2008_12
						&var._2009_1-&var._2009_12
						&var._2010_1-&var._2010_12
						&var._2011_1-&var._2011_12
						&var._2012_1-&var._2012_12;
	%mend array1d;

	%macro array2d(var);
	array &var.(1983:2012, 1:12) 	&var._1983_1-&var._1983_12
									&var._1984_1-&var._1984_12
									&var._1985_1-&var._1985_12
									&var._1986_1-&var._1986_12
									&var._1987_1-&var._1987_12
									&var._1988_1-&var._1988_12
									&var._1989_1-&var._1989_12
									&var._1990_1-&var._1990_12
									&var._1991_1-&var._1991_12
									&var._1992_1-&var._1992_12
									&var._1993_1-&var._1993_12
									&var._1994_1-&var._1994_12
									&var._1995_1-&var._1995_12
									&var._1996_1-&var._1996_12
									&var._1997_1-&var._1997_12
									&var._1998_1-&var._1998_12
									&var._1999_1-&var._1999_12
									&var._2000_1-&var._2000_12
									&var._2001_1-&var._2001_12
									&var._2002_1-&var._2002_12
									&var._2003_1-&var._2003_12
									&var._2004_1-&var._2004_12
									&var._2005_1-&var._2005_12
									&var._2006_1-&var._2006_12
									&var._2007_1-&var._2007_12
									&var._2008_1-&var._2008_12
									&var._2009_1-&var._2009_12
									&var._2010_1-&var._2010_12
									&var._2011_1-&var._2011_12
									&var._2012_1-&var._2012_12;
  	%mend array2d;


	%macro drop(var);
	drop 	&var._1983_1-&var._1983_12
			&var._1984_1-&var._1984_12
			&var._1985_1-&var._1985_12
			&var._1986_1-&var._1986_12
			&var._1987_1-&var._1987_12
			&var._1988_1-&var._1988_12
			&var._1989_1-&var._1989_12
			&var._1990_1-&var._1990_12
			&var._1991_1-&var._1991_12
			&var._1992_1-&var._1992_12
			&var._1993_1-&var._1993_12
			&var._1994_1-&var._1994_12
			&var._1995_1-&var._1995_12
			&var._1996_1-&var._1996_12
			&var._1997_1-&var._1997_12
			&var._1998_1-&var._1998_12
			&var._1999_1-&var._1999_12
			&var._2000_1-&var._2000_12
			&var._2001_1-&var._2001_12
			&var._2002_1-&var._2002_12
			&var._2003_1-&var._2003_12
			&var._2004_1-&var._2004_12
			&var._2005_1-&var._2005_12
			&var._2006_1-&var._2006_12
			&var._2007_1-&var._2007_12
			&var._2008_1-&var._2008_12
			&var._2009_1-&var._2009_12
			&var._2010_1-&var._2010_12
			&var._2011_1-&var._2011_12
			&var._2012_1-&var._2012_12;
	%mend drop;


* CREATE MONTHS ON SSI OVER LIFETIME (USING FEDPMT VARIABLE);

	%array2d(fedpmt);
	%array2d(statpmt);
	%array2d(n_payind);
	
	array paymon(1983:2012) n_paymonyr_1983-n_paymonyr_2012;
	
	* Create an indicator for each month payment received, 
	then sum the number of months of payment in each year,
	then sum the total number months of payment (across all years);
	do i = 1983 to 2012;
		do j = 1 to 12;
			if fedpmt(i,j)>0 
			then n_payind(i,j)=1;
				else n_payind(i,j)=0;
		end;
		paymon(i) = SUM(n_payind(i,1),n_payind(i,2),n_payind(i,3),n_payind(i,4),
						n_payind(i,5),n_payind(i,6),n_payind(i,7),n_payind(i,8),
						n_payind(i,9),n_payind(i,10),n_payind(i,11),n_payind(i,12));
	end;

	%drop(n_payind);

	n_paymontot = SUM(OF n_paymonyr_1983-n_paymonyr_2012);
	label n_paymontot='Total months of payment (lifetime)';

	*drop n_paymonyr_1983-n_paymonyr_2012;


* CREATE MONTHS ON SSI BEFORE AGE 18 (USING FEDPMT VARIABLE);
	n_dob_char = put(dob,$8.); 

	n_dobmm = substr(n_dob_char,5,2); 
	n_dobmm = input(n_dobmm,2.0); 
	label n_dobmm='Month of birth';

	n_dobdd = substr(n_dob_char,7,2);
	n_dobdd = input(n_dobdd,2.0);
	label n_dobdd='Day of birth';
	
	* Calculate year and month in which turned 18;
	n_age18_yr=dobyy+18;
	label n_age18_yr='Year turned 18';
	n_age18_mo=n_dobmm;
	label n_age18_mo='Month turned 18';
	n_age18_day=n_dobdd;
	label n_age18_day='Day turned 18';
	n_age18_date=n_age18_yr+(n_age18_mo/12);
	label n_age18_date='Date turned 18';

	%array2d(n_payind18);
	array paymon18(1983:2012) n_paymon18yr_1983-n_paymon18yr_2012;

	do i = 1983 to 2012;
		do j = 1 to 12;
			if 
			fedpmt(i,j)>0 & (i<n_age18_yr or (i=n_age18_yr & j<n_age18_mo)) 
			then 
			n_payind18(i,j)=1;
				else n_payind18(i,j)=0;
		end;
		paymon18(i) = SUM(n_payind18(i,1),n_payind18(i,2),n_payind18(i,3),n_payind18(i,4),
						n_payind18(i,5),n_payind18(i,6),n_payind18(i,7),n_payind18(i,8),
						n_payind18(i,9),n_payind18(i,10),n_payind18(i,11),n_payind18(i,12));
	end;

	n_paymontot18 = SUM(OF n_paymon18yr_1983-n_paymon18yr_2012);
	label n_paymontot18='Total months of payment (age 18)';

	drop n_paymon18yr_1983-n_paymon18yr_2012;


* CREATE TOTAL PAYMENT OVER LIFETIME USING FEDPMT AND STATPMT
		(the variables that give ACTUAL payment received);
	
	* For each year, sum monthly payments (both fed and state);
	array fedpay(1983:2012) n_fedpay_1983-n_fedpay_2012;
	array statpay(1983:2012) n_statpay_1983-n_statpay_2012;
	do i = 1983 to 2012;
			fedpay(i) = SUM(fedpmt(i,1),fedpmt(i,2),fedpmt(i,3),fedpmt(i,4),
						fedpmt(i,5),fedpmt(i,6),fedpmt(i,7),fedpmt(i,8),
						fedpmt(i,9),fedpmt(i,10),fedpmt(i,11),fedpmt(i,12));
			statpay(i) = SUM(statpmt(i,1),statpmt(i,2),statpmt(i,3),statpmt(i,4),
						statpmt(i,5),statpmt(i,6),statpmt(i,7),statpmt(i,8),
						statpmt(i,9),statpmt(i,10),statpmt(i,11),statpmt(i,12));
	end;
	
	* Now inflate each year's payments to 2012 dollars;
	n_cpi_1983=2.30;
	n_cpi_1984=2.21;
	n_cpi_1985=2.13;
	n_cpi_1986=2.09;
	n_cpi_1987=2.03;
	n_cpi_1988=1.94;
	n_cpi_1989=1.86;
	n_cpi_1990=1.76;
	n_cpi_1991=1.69;
	n_cpi_1992=1.64;
	n_cpi_1993=1.59;
	n_cpi_1994=1.55;
	n_cpi_1995=1.51;
	n_cpi_1996=1.47;
	n_cpi_1997=1.43;
	n_cpi_1998=1.41;
	n_cpi_1999=1.38;
	n_cpi_2000=1.34;
	n_cpi_2001=1.3;
	n_cpi_2002=1.28;
	n_cpi_2003=1.25;
	n_cpi_2004=1.22;
	n_cpi_2005=1.18;
	n_cpi_2006=1.14;
	n_cpi_2007=1.11;
	n_cpi_2008=1.07;
	n_cpi_2009=1.07;
	n_cpi_2010=1.06;
	n_cpi_2011=1.02;
	n_cpi_2012=1;

	array cpi(1983:2012) n_cpi_1983-n_cpi_2012;
	do i = 1983 to 2012;
		fedpay(i) = fedpay(i)*cpi(i);
		statpay(i) = statpay(i)*cpi(i);
	end;
	
	array combpay(1983:2012) n_combpayyr_1983-n_combpayyr_2012;
	do i = 1983 to 2012;
		combpay(i)=fedpay(i)+statpay(i);
	end;

	* Calculate lifetime totals: fed, state, combined;
	n_fedpaytot=SUM(OF n_fedpay_:);
	label n_fedpaytot='Total federal SSI payments (lifetime)';
	n_statpaytot=SUM(OF n_statpay_:);
	label n_statpaytot='Total state SSI payments (lifetime)';
	n_combpaytot=n_fedpaytot + n_statpaytot;
	label n_combpaytot='Total comb fed & state SSI payments (lifetime)';

	drop n_fedpay_1983-n_fedpay_2012;
	drop n_statpay_1983-n_statpay_2012;

* CREATE TOTAL PAYMENT BEFORE AGE 18 USING FEDPMT AND STATPMT;
	
	* Create new variables that equal fed/state payments if 
	payments occurred before age 18, equal 0 else;
	%array2d(n_fedpmt18);
	%array2d(n_statpmt18);
	
	do i = 1983 to 2012;
		do j = 1 to 12;
			n_fedpmt18(i,j)=fedpmt(i,j)*n_payind18(i,j);
			n_statpmt18(i,j)=statpmt(i,j)*n_payind18(i,j);
		end;
	end;

	* Sum them over years, just like before;
	array fedpay18(1983:2012) n_fedpay18_1983-n_fedpay18_2012;
	array statpay18(1983:2012) n_statpay18_1983-n_statpay18_2012;
	do i = 1983 to 2012;
			fedpay18(i) = SUM(n_fedpmt18(i,1),n_fedpmt18(i,2),n_fedpmt18(i,3),n_fedpmt18(i,4),
						n_fedpmt18(i,5),n_fedpmt18(i,6),n_fedpmt18(i,7),n_fedpmt18(i,8),
						n_fedpmt18(i,9),n_fedpmt18(i,10),n_fedpmt18(i,11),n_fedpmt18(i,12));
			statpay18(i) = SUM(n_statpmt18(i,1),n_statpmt18(i,2),n_statpmt18(i,3),n_statpmt18(i,4),
						n_statpmt18(i,5),n_statpmt18(i,6),n_statpmt18(i,7),n_statpmt18(i,8),
						n_statpmt18(i,9),n_statpmt18(i,10),n_statpmt18(i,11),n_statpmt18(i,12));
	end;

	%drop(n_fedpmt18);
	%drop(n_statpmt18);

	* Now inflate each year's payments to 2012 dollars;
	do i = 1983 to 2012;
		fedpay18(i) = fedpay18(i)*cpi(i);
		statpay18(i) = statpay18(i)*cpi(i);
	end;

	* Calculate age 18 totals: fed, state, combined;
	n_fedpaytot18=SUM(OF n_fedpay18_:);
	label n_fedpaytot18='Total federal SSI payments (up to 18)';
	n_statpaytot18=SUM(OF n_statpay18_:);
	label n_statpaytot18='Total state SSI payments (up to 18)';
	n_combpaytot18=n_fedpaytot18 + n_statpaytot18;
	label n_combpaytot18='Total comb fed & state SSI payments (up to 18)';
	
	%drop(n_payind18);
	drop n_fedpay18_1983-n_fedpay18_2012;
	drop n_statpay18_1983-n_statpay18_2012;


* FIND DATE AND AGE AT FIRST PAYMENT;
	%array2d(n_temp);
	%array2d(n_first);
	
	* Find date of first payment ;
	n_first_s=0;
	do i = 1983 to 2012;
		do j = 1 to 12;
			n_temp(i,j) = SUM(OF n_first_:);
			if fedpmt(i,j)>0 and n_temp(i,j)=0 then n_first(i,j)=1;
				else n_first(i,j)=0;
		end;
	end;

	%drop(n_temp);
	
	n_firstpay_yr=.;
	n_firstpay_mo=.;
	do i = 1983 to 2012;
		do j = 1 to 12;
			if n_first(i,j)=1 then n_firstpay_yr=i;
			if n_first(i,j)=1 then n_firstpay_mo=j;
		end;
	end;
	label n_firstpay_yr='Year of first payment';
	label n_firstpay_mo='Month of first payment';

	%drop(n_first);
	drop n_first_s;

	* Calculate age at first payment;
	n_firstage = (n_firstpay_yr - dobyy) + ((n_firstpay_mo-n_dobmm)/12);
	label n_firstage='Age at first payment';

	drop n_dob_char;

* CREATE INDICATOR FOR UNFAVORABLE CDR DECISION (WHICH MAY OR MAY NOT RESULT IN REMOVAL);
	%array1d(n_cdrunfav);
	%array1d(pstat);

	* Is PSTAT ever an unfavorable code?;
	do i = 1 to 360;
		if  
			(pstat1(i)='N07' or pstat1(i)='N08')
		then n_cdrunfav1(i)=1;
		else n_cdrunfav1(i)=0;
	end;
	
	* Create indicator for ever unfavorable CDR decision;
	n_cdrunfav_temp = SUM(OF n_cdrunfav_:);
	if n_cdrunfav_temp>0 then n_cdr_unfav=1;
		else n_cdr_unfav=0;
	drop n_cdrunfav_:;
	label n_cdr_unfav='Unfavorable CDR in lifetime';

	* Create date of first unfavorable CDR decision;
	n_cdrunfav1_date=.;
	do i = 2 to 360;
		if  
			(pstat1(i)='N07' or pstat1(i)='N08')
		and
			(pstat1(i-1)^='N07' and pstat1(i-1)^='N08')
		and 
			(n_cdrunfav1_date=.)
		then do;
			n_cdrunfav1_date=i;
		end;
	end;

	* Create year and month of second unfavorable CDR decision;
	n_cdrunfav2_date=.;
	do i = 2 to 360;
		if  
			(n_cdrunfav1_date>.)
		and
			(n_cdrunfav1_date<i) 
		and 
			(pstat1(i)='N07' or pstat1(i)='N08')
		and
			(pstat1(i-1)^='N07' and pstat1(i-1)^='N08')
		and 
			(n_cdrunfav2_date=.)
		then do;
			n_cdrunfav2_date=i;
		end;
	end;

	n_cdrunfav1_date = (n_cdrunfav1_date/12) + 1983;
	n_cdrunfav2_date = (n_cdrunfav2_date/12) + 1983;

	label n_cdrunfav1_date='Date of adverse CDR 1 (lifetime)';
	label n_cdrunfav2_date='Date of adverse CDR 2 (lifetime)';

	* Create dummy for whether unfavorable CDR before age 18;
	if 
		n_cdrunfav1_date^=.
	and
		(n_cdrunfav1_date<n_age18_date)
	then 
		n_cdr_unfav18=1;
	else
		n_cdr_unfav18=0;

	label n_cdr_unfav18='Unfavorable CDR before age 18';
	

	* Create dummies for unfavorable CDR in each FY;
	%macro cdr(date1,date2,year);
		if 
			(n_cdrunfav1_date>&date1. and n_cdrunfav1_date<=&date2.)
		or
			(n_cdrunfav2_date>&date1. and n_cdrunfav2_date<=&date2.)
		then 
			n_cdrunfavyear_&year.=1;
		else
			n_cdrunfavyear_&year.=0;
	%mend cdr;

	%cdr(1990.75,1991.75,1991);
	%cdr(1991.75,1992.75,1992);
	%cdr(1992.75,1993.75,1993);
	%cdr(1993.75,1994.75,1994);
	%cdr(1994.75,1995.75,1995);
	%cdr(1995.75,1996.75,1996);
	%cdr(1996.75,1997.75,1997);
	%cdr(1997.75,1998.75,1998);
	%cdr(1998.75,1999.75,1999);
	%cdr(1999.75,2000.75,2000);
	%cdr(2000.75,2001.75,2001);
	%cdr(2001.75,2002.75,2002);
	%cdr(2002.75,2003.75,2003);
	%cdr(2003.75,2004.75,2004);
	%cdr(2004.75,2005.75,2005);
	%cdr(2005.75,2006.75,2006);
	%cdr(2006.75,2007.75,2007);
	%cdr(2007.75,2008.75,2008);
	%cdr(2008.75,2009.75,2009);
	%cdr(2009.75,2010.75,2010);
	%cdr(2010.75,2011.75,2011);
	%cdr(2011.75,2012.75,2012);



* CREATE DATES FOR OTHER MAJOR TERMINATION REASONS;

	* N01 (INCOME);
	n_N01unfav1_date=.;
	do i = 2 to 360;
		if  pstat1(i)='N01' and pstat1(i-1)^='N01' and n_N01unfav1_date=.
		then do;
			n_N01unfav1_date=i;
		end;
	end;

	n_N01unfav2_date=.;
	do i = 2 to 360;
		if  n_N01unfav1_date>. and n_N01unfav1_date<i-6 and 
			pstat1(i)='N01' and pstat1(i-1)^='N01' and n_N01unfav2_date=.
		then do;
			n_N01unfav2_date=i;
		end;
	end;

	n_N01unfav3_date=.;
	do i = 2 to 360;
		if  n_N01unfav1_date>. and n_N01unfav1_date<i-6 and 
			n_N01unfav2_date>. and n_N01unfav2_date<i-6 and 
			pstat1(i)='N01' and pstat1(i-1)^='N01' and n_N01unfav3_date=.
		then do;
			n_N01unfav3_date=i;
		end;
	end;

	n_N01unfav4_date=.;
	do i = 2 to 360;
		if  n_N01unfav1_date>. and n_N01unfav1_date<i-6 and 
			n_N01unfav2_date>. and n_N01unfav2_date<i-6 and 
			n_N01unfav3_date>. and n_N01unfav3_date<i-6 and 
			pstat1(i)='N01' and pstat1(i-1)^='N01' and n_N01unfav4_date=.
		then do;
			n_N01unfav4_date=i;
		end;
	end;

	n_N01unfav5_date=.;
	do i = 2 to 360;
		if  n_N01unfav1_date>. and n_N01unfav1_date<i-6 and 
			n_N01unfav2_date>. and n_N01unfav2_date<i-6 and 
			n_N01unfav3_date>. and n_N01unfav3_date<i-6 and 
			n_N01unfav4_date>. and n_N01unfav4_date<i-6 and 
			pstat1(i)='N01' and pstat1(i-1)^='N01' and n_N01unfav5_date=.
		then do;
			n_N01unfav5_date=i;
		end;
	end;

	n_N01unfav6_date=.;
	do i = 2 to 360;
		if  n_N01unfav1_date>. and n_N01unfav1_date<i-6 and 
			n_N01unfav2_date>. and n_N01unfav2_date<i-6 and 
			n_N01unfav3_date>. and n_N01unfav3_date<i-6 and 
			n_N01unfav4_date>. and n_N01unfav4_date<i-6 and 
			n_N01unfav5_date>. and n_N01unfav5_date<i-6 and 
			pstat1(i)='N01' and pstat1(i-1)^='N01' and n_N01unfav6_date=.
		then do;
			n_N01unfav6_date=i;
		end;
	end;

	n_N01unfav7_date=.;
	do i = 2 to 360;
		if  n_N01unfav1_date>. and n_N01unfav1_date<i-6 and 
			n_N01unfav2_date>. and n_N01unfav2_date<i-6 and 
			n_N01unfav3_date>. and n_N01unfav3_date<i-6 and 
			n_N01unfav4_date>. and n_N01unfav4_date<i-6 and 
			n_N01unfav5_date>. and n_N01unfav5_date<i-6 and 
			n_N01unfav6_date>. and n_N01unfav6_date<i-6 and 
			pstat1(i)='N01' and pstat1(i-1)^='N01' and n_N01unfav7_date=.
		then do;
			n_N01unfav7_date=i;
		end;
	end;

	n_N01unfav8_date=.;
	do i = 2 to 360;
		if  n_N01unfav1_date>. and n_N01unfav1_date<i-6 and 
			n_N01unfav2_date>. and n_N01unfav2_date<i-6 and 
			n_N01unfav3_date>. and n_N01unfav3_date<i-6 and 
			n_N01unfav4_date>. and n_N01unfav4_date<i-6 and 
			n_N01unfav5_date>. and n_N01unfav5_date<i-6 and 
			n_N01unfav6_date>. and n_N01unfav6_date<i-6 and 
			n_N01unfav7_date>. and n_N01unfav7_date<i-6 and 
			pstat1(i)='N01' and pstat1(i-1)^='N01' and n_N01unfav8_date=.
		then do;
			n_N01unfav8_date=i;
		end;
	end;

	n_N01unfav9_date=.;
	do i = 2 to 360;
		if  n_N01unfav1_date>. and n_N01unfav1_date<i-6 and 
			n_N01unfav2_date>. and n_N01unfav2_date<i-6 and 
			n_N01unfav3_date>. and n_N01unfav3_date<i-6 and 
			n_N01unfav4_date>. and n_N01unfav4_date<i-6 and 
			n_N01unfav5_date>. and n_N01unfav5_date<i-6 and 
			n_N01unfav6_date>. and n_N01unfav6_date<i-6 and 
			n_N01unfav7_date>. and n_N01unfav7_date<i-6 and 
			n_N01unfav8_date>. and n_N01unfav8_date<i-6 and 
			pstat1(i)='N01' and pstat1(i-1)^='N01' and n_N01unfav9_date=.
		then do;
			n_N01unfav9_date=i;
		end;
	end;

	n_N01unfav10_date=.;
	do i = 2 to 360;
		if  n_N01unfav1_date>. and n_N01unfav1_date<i-6 and 
			n_N01unfav2_date>. and n_N01unfav2_date<i-6 and 
			n_N01unfav3_date>. and n_N01unfav3_date<i-6 and 
			n_N01unfav4_date>. and n_N01unfav4_date<i-6 and 
			n_N01unfav5_date>. and n_N01unfav5_date<i-6 and 
			n_N01unfav6_date>. and n_N01unfav6_date<i-6 and 
			n_N01unfav7_date>. and n_N01unfav7_date<i-6 and 
			n_N01unfav8_date>. and n_N01unfav8_date<i-6 and 
			n_N01unfav9_date>. and n_N01unfav9_date<i-6 and 
			pstat1(i)='N01' and pstat1(i-1)^='N01' and n_N01unfav10_date=.
		then do;
			n_N01unfav10_date=i;
		end;
	end;

	n_N01unfav1_date = (n_N01unfav1_date/12) + 1983;
	n_N01unfav2_date = (n_N01unfav2_date/12) + 1983;
	n_N01unfav3_date = (n_N01unfav3_date/12) + 1983;
	n_N01unfav4_date = (n_N01unfav4_date/12) + 1983;
	n_N01unfav5_date = (n_N01unfav5_date/12) + 1983;
	n_N01unfav6_date = (n_N01unfav6_date/12) + 1983;
	n_N01unfav7_date = (n_N01unfav7_date/12) + 1983;
	n_N01unfav8_date = (n_N01unfav8_date/12) + 1983;
	n_N01unfav9_date = (n_N01unfav9_date/12) + 1983;
	n_N01unfav10_date = (n_N01unfav10_date/12) + 1983;

	%macro n01(date1,date2,year);
		if 
			(n_N01unfav1_date>&date1. and n_N01unfav1_date<=&date2.)
		or
			(n_N01unfav2_date>&date1. and n_N01unfav2_date<=&date2.)
		or 
			(n_N01unfav3_date>&date1. and n_N01unfav3_date<=&date2.)
		or 
			(n_N01unfav4_date>&date1. and n_N01unfav4_date<=&date2.)
		or 
			(n_N01unfav5_date>&date1. and n_N01unfav5_date<=&date2.)
		or 
			(n_N01unfav6_date>&date1. and n_N01unfav6_date<=&date2.)
		or 
			(n_N01unfav7_date>&date1. and n_N01unfav7_date<=&date2.)
		or 
			(n_N01unfav8_date>&date1. and n_N01unfav8_date<=&date2.)
		or 
			(n_N01unfav9_date>&date1. and n_N01unfav9_date<=&date2.)
		or 
			(n_N01unfav10_date>&date1. and n_N01unfav10_date<=&date2.)
		then 
			n_N01unfavyear_&year.=1;
		else
			n_N01unfavyear_&year.=0;
	%mend n01;

	%n01(1990.75,1991.75,1991);
	%n01(1991.75,1992.75,1992);
	%n01(1992.75,1993.75,1993);
	%n01(1993.75,1994.75,1994);
	%n01(1994.75,1995.75,1995);
	%n01(1995.75,1996.75,1996);
	%n01(1996.75,1997.75,1997);
	%n01(1997.75,1998.75,1998);
	%n01(1998.75,1999.75,1999);
	%n01(1999.75,2000.75,2000);
	%n01(2000.75,2001.75,2001);
	%n01(2001.75,2002.75,2002);
	%n01(2002.75,2003.75,2003);
	%n01(2003.75,2004.75,2004);
	%n01(2004.75,2005.75,2005);
	%n01(2005.75,2006.75,2006);
	%n01(2006.75,2007.75,2007);
	%n01(2007.75,2008.75,2008);
	%n01(2008.75,2009.75,2009);
	%n01(2009.75,2010.75,2010);
	%n01(2010.75,2011.75,2011);
	%n01(2011.75,2012.75,2012);

	* N04 ();
	n_N04unfav1_date=.;
	do i = 2 to 360;
		if  pstat1(i)='N04' and pstat1(i-1)^='N04' and n_N04unfav1_date=.
		then do;
			n_N04unfav1_date=i;
		end;
	end;

	n_N04unfav2_date=.;
	do i = 2 to 360;
		if  n_N04unfav1_date>. and n_N04unfav1_date<i and 
			pstat1(i)='N04' and pstat1(i-1)^='N04' and n_N04unfav2_date=.
		then do;
			n_N04unfav2_date=i;
		end;
	end;

	n_N04unfav3_date=.;
	do i = 2 to 360;
		if  n_N04unfav1_date>. and n_N04unfav1_date<i and 
			n_N04unfav2_date>. and n_N04unfav2_date<i and 
			pstat1(i)='N04' and pstat1(i-1)^='N04' and n_N04unfav3_date=.
		then do;
			n_N04unfav3_date=i;
		end;
	end;

	n_N04unfav4_date=.;
	do i = 2 to 360;
		if  n_N04unfav1_date>. and n_N04unfav1_date<i and 
			n_N04unfav2_date>. and n_N04unfav2_date<i and 
			n_N04unfav3_date>. and n_N04unfav3_date<i and 
			pstat1(i)='N04' and pstat1(i-1)^='N04' and n_N04unfav4_date=.
		then do;
			n_N04unfav4_date=i;
		end;
	end;

	n_N04unfav1_date = (n_N04unfav1_date/12) + 1983;
	n_N04unfav2_date = (n_N04unfav2_date/12) + 1983;
	n_N04unfav3_date = (n_N04unfav3_date/12) + 1983;
	n_N04unfav4_date = (n_N04unfav4_date/12) + 1983;

	label n_N04unfav1_date='Date of adverse N04 (lifetime)';
	label n_N04unfav2_date='Date of adverse N04 (lifetime)';
	label n_N04unfav3_date='Date of adverse N04 (lifetime)';
	label n_N04unfav4_date='Date of adverse N04 (lifetime)';

	%macro n04(date1,date2,year);
		if 
			(n_N04unfav1_date>&date1. and n_N04unfav1_date<=&date2.)
		or
			(n_N04unfav2_date>&date1. and n_N04unfav2_date<=&date2.)
		or
			(n_N04unfav3_date>&date1. and n_N04unfav3_date<=&date2.)
		or
			(n_N04unfav4_date>&date1. and n_N04unfav4_date<=&date2.)
		then 
			n_N04unfavyear_&year.=1;
		else
			n_N04unfavyear_&year.=0;
	%mend n04;

	%n04(1990.75,1991.75,1991);
	%n04(1991.75,1992.75,1992);
	%n04(1992.75,1993.75,1993);
	%n04(1993.75,1994.75,1994);
	%n04(1994.75,1995.75,1995);
	%n04(1995.75,1996.75,1996);
	%n04(1996.75,1997.75,1997);
	%n04(1997.75,1998.75,1998);
	%n04(1998.75,1999.75,1999);
	%n04(1999.75,2000.75,2000);
	%n04(2000.75,2001.75,2001);
	%n04(2001.75,2002.75,2002);
	%n04(2002.75,2003.75,2003);
	%n04(2003.75,2004.75,2004);
	%n04(2004.75,2005.75,2005);
	%n04(2005.75,2006.75,2006);
	%n04(2006.75,2007.75,2007);
	%n04(2007.75,2008.75,2008);
	%n04(2008.75,2009.75,2009);
	%n04(2009.75,2010.75,2010);
	%n04(2010.75,2011.75,2011);
	%n04(2011.75,2012.75,2012);

	* N20 ();
	n_N20unfav1_date=.;
	do i = 2 to 360;
		if  pstat1(i)='N20' and pstat1(i-1)^='N20' and n_N20unfav1_date=.
		then do;
			n_N20unfav1_date=i;
		end;
	end;

	n_N20unfav2_date=.;
	do i = 2 to 360;
		if  n_N20unfav1_date>. and n_N20unfav1_date<i and 
			pstat1(i)='N20' and pstat1(i-1)^='N20' and n_N20unfav2_date=.
		then do;
			n_N20unfav2_date=i;
		end;
	end;

	n_N20unfav3_date=.;
	do i = 2 to 360;
		if  n_N20unfav1_date>. and n_N20unfav1_date<i and 
			n_N20unfav2_date>. and n_N20unfav2_date<i and 
			pstat1(i)='N20' and pstat1(i-1)^='N20' and n_N20unfav3_date=.
		then do;
			n_N20unfav3_date=i;
		end;
	end;

	n_N20unfav4_date=.;
	do i = 2 to 360;
		if  n_N20unfav1_date>. and n_N20unfav1_date<i and 
			n_N20unfav2_date>. and n_N20unfav2_date<i and 
			n_N20unfav3_date>. and n_N20unfav3_date<i and 
			pstat1(i)='N20' and pstat1(i-1)^='N20' and n_N20unfav4_date=.
		then do;
			n_N20unfav4_date=i;
		end;
	end;

	n_N20unfav1_date = (n_N20unfav1_date/12) + 1983;
	n_N20unfav2_date = (n_N20unfav2_date/12) + 1983;
	n_N20unfav3_date = (n_N20unfav3_date/12) + 1983;
	n_N20unfav4_date = (n_N20unfav4_date/12) + 1983;

	label n_N20unfav1_date='Date of adverse N20 (lifetime)';
	label n_N20unfav2_date='Date of adverse N20 (lifetime)';
	label n_N20unfav3_date='Date of adverse N20 (lifetime)';
	label n_N20unfav4_date='Date of adverse N20 (lifetime)';

	%macro n20(date1,date2,year);
		if 
			(n_N20unfav1_date>&date1. and n_N20unfav1_date<=&date2.)
		or
			(n_N20unfav2_date>&date1. and n_N20unfav2_date<=&date2.)
		or
			(n_N20unfav3_date>&date1. and n_N20unfav3_date<=&date2.)
		or
			(n_N20unfav4_date>&date1. and n_N20unfav4_date<=&date2.)
		then 
			n_N20unfavyear_&year.=1;
		else
			n_N20unfavyear_&year.=0;
	%mend n20;

	%n20(1990.75,1991.75,1991);
	%n20(1991.75,1992.75,1992);
	%n20(1992.75,1993.75,1993);
	%n20(1993.75,1994.75,1994);
	%n20(1994.75,1995.75,1995);
	%n20(1995.75,1996.75,1996);
	%n20(1996.75,1997.75,1997);
	%n20(1997.75,1998.75,1998);
	%n20(1998.75,1999.75,1999);
	%n20(1999.75,2000.75,2000);
	%n20(2000.75,2001.75,2001);
	%n20(2001.75,2002.75,2002);
	%n20(2002.75,2003.75,2003);
	%n20(2003.75,2004.75,2004);
	%n20(2004.75,2005.75,2005);
	%n20(2005.75,2006.75,2006);
	%n20(2006.75,2007.75,2007);
	%n20(2007.75,2008.75,2008);
	%n20(2008.75,2009.75,2009);
	%n20(2009.75,2010.75,2010);
	%n20(2010.75,2011.75,2011);
	%n20(2011.75,2012.75,2012);

	* N40 ();
	n_N40unfav1_date=.;
	do i = 2 to 360;
		if  pstat1(i)='N40' and pstat1(i-1)^='N40' and n_N40unfav1_date=.
		then do;
			n_N40unfav1_date=i;
		end;
	end;

	n_N40unfav2_date=.;
	do i = 2 to 360;
		if  n_N40unfav1_date>. and n_N40unfav1_date<i and 
			pstat1(i)='N40' and pstat1(i-1)^='N40' and n_N40unfav2_date=.
		then do;
			n_N40unfav2_date=i;
		end;
	end;

	n_N40unfav3_date=.;
	do i = 2 to 360;
		if  n_N40unfav1_date>. and n_N40unfav1_date<i and 
			n_N40unfav2_date>. and n_N40unfav2_date<i and 
			pstat1(i)='N40' and pstat1(i-1)^='N40' and n_N40unfav3_date=.
		then do;
			n_N40unfav3_date=i;
		end;
	end;

	n_N40unfav4_date=.;
	do i = 2 to 360;
		if  n_N40unfav1_date>. and n_N40unfav1_date<i and 
			n_N40unfav2_date>. and n_N40unfav2_date<i and 
			n_N40unfav3_date>. and n_N40unfav3_date<i and 
			pstat1(i)='N40' and pstat1(i-1)^='N40' and n_N40unfav4_date=.
		then do;
			n_N40unfav4_date=i;
		end;
	end;

	n_N40unfav1_date = (n_N40unfav1_date/12) + 1983;
	n_N40unfav2_date = (n_N40unfav2_date/12) + 1983;
	n_N40unfav3_date = (n_N40unfav3_date/12) + 1983;
	n_N40unfav4_date = (n_N40unfav4_date/12) + 1983;

	label n_N40unfav1_date='Date of adverse N40 (lifetime)';
	label n_N40unfav2_date='Date of adverse N40 (lifetime)';
	label n_N40unfav3_date='Date of adverse N40 (lifetime)';
	label n_N40unfav4_date='Date of adverse N40 (lifetime)';

	%macro n40(date1,date2,year);
		if 
			(n_N40unfav1_date>&date1. and n_N40unfav1_date<=&date2.)
		or
			(n_N40unfav2_date>&date1. and n_N40unfav2_date<=&date2.)
		or
			(n_N40unfav3_date>&date1. and n_N40unfav3_date<=&date2.)
		or
			(n_N40unfav4_date>&date1. and n_N40unfav4_date<=&date2.)
		then 
			n_N40unfavyear_&year.=1;
		else
			n_N40unfavyear_&year.=0;
	%mend n40;

	%n40(1990.75,1991.75,1991);
	%n40(1991.75,1992.75,1992);
	%n40(1992.75,1993.75,1993);
	%n40(1993.75,1994.75,1994);
	%n40(1994.75,1995.75,1995);
	%n40(1995.75,1996.75,1996);
	%n40(1996.75,1997.75,1997);
	%n40(1997.75,1998.75,1998);
	%n40(1998.75,1999.75,1999);
	%n40(1999.75,2000.75,2000);
	%n40(2000.75,2001.75,2001);
	%n40(2001.75,2002.75,2002);
	%n40(2002.75,2003.75,2003);
	%n40(2003.75,2004.75,2004);
	%n40(2004.75,2005.75,2005);
	%n40(2005.75,2006.75,2006);
	%n40(2006.75,2007.75,2007);
	%n40(2007.75,2008.75,2008);
	%n40(2008.75,2009.75,2009);
	%n40(2009.75,2010.75,2010);
	%n40(2010.75,2011.75,2011);
	%n40(2011.75,2012.75,2012);


	* N43 ();
	n_N43unfav1_date=.;
	do i = 2 to 360;
		if  pstat1(i)='N43' and pstat1(i-1)^='N43' and n_N43unfav1_date=.
		then do;
			n_N43unfav1_date=i;
		end;
	end;

	n_N43unfav2_date=.;
	do i = 2 to 360;
		if  n_N43unfav1_date>. and n_N43unfav1_date<i and 
			pstat1(i)='N43' and pstat1(i-1)^='N43' and n_N43unfav2_date=.
		then do;
			n_N43unfav2_date=i;
		end;
	end;

	n_N43unfav3_date=.;
	do i = 2 to 360;
		if  n_N43unfav1_date>. and n_N43unfav1_date<i and 
			n_N43unfav2_date>. and n_N43unfav2_date<i and 
			pstat1(i)='N43' and pstat1(i-1)^='N43' and n_N43unfav3_date=.
		then do;
			n_N43unfav3_date=i;
		end;
	end;

	n_N43unfav4_date=.;
	do i = 2 to 360;
		if  n_N43unfav1_date>. and n_N43unfav1_date<i and 
			n_N43unfav2_date>. and n_N43unfav2_date<i and 
			n_N43unfav3_date>. and n_N43unfav3_date<i and 
			pstat1(i)='N43' and pstat1(i-1)^='N43' and n_N43unfav4_date=.
		then do;
			n_N43unfav4_date=i;
		end;
	end;

	n_N43unfav1_date = (n_N43unfav1_date/12) + 1983;
	n_N43unfav2_date = (n_N43unfav2_date/12) + 1983;
	n_N43unfav3_date = (n_N43unfav3_date/12) + 1983;
	n_N43unfav4_date = (n_N43unfav4_date/12) + 1983;

	label n_N43unfav1_date='Date of adverse N43 (lifetime)';
	label n_N43unfav2_date='Date of adverse N43 (lifetime)';
	label n_N43unfav3_date='Date of adverse N43 (lifetime)';
	label n_N43unfav4_date='Date of adverse N43 (lifetime)';

	%macro n43(date1,date2,year);
		if 
			(n_N43unfav1_date>&date1. and n_N43unfav1_date<=&date2.)
		or
			(n_N43unfav2_date>&date1. and n_N43unfav2_date<=&date2.)
		or
			(n_N43unfav3_date>&date1. and n_N43unfav3_date<=&date2.)
		or
			(n_N43unfav4_date>&date1. and n_N43unfav4_date<=&date2.)
		then 
			n_N43unfavyear_&year.=1;
		else
			n_N43unfavyear_&year.=0;
	%mend n43;

	%n43(1990.75,1991.75,1991);
	%n43(1991.75,1992.75,1992);
	%n43(1992.75,1993.75,1993);
	%n43(1993.75,1994.75,1994);
	%n43(1994.75,1995.75,1995);
	%n43(1995.75,1996.75,1996);
	%n43(1996.75,1997.75,1997);
	%n43(1997.75,1998.75,1998);
	%n43(1998.75,1999.75,1999);
	%n43(1999.75,2000.75,2000);
	%n43(2000.75,2001.75,2001);
	%n43(2001.75,2002.75,2002);
	%n43(2002.75,2003.75,2003);
	%n43(2003.75,2004.75,2004);
	%n43(2004.75,2005.75,2005);
	%n43(2005.75,2006.75,2006);
	%n43(2006.75,2007.75,2007);
	%n43(2007.75,2008.75,2008);
	%n43(2008.75,2009.75,2009);
	%n43(2009.75,2010.75,2010);
	%n43(2010.75,2011.75,2011);
	%n43(2011.75,2012.75,2012);

	* N44 ();
	n_N44unfav1_date=.;
	do i = 2 to 360;
		if  pstat1(i)='N44' and pstat1(i-1)^='N44' and n_N44unfav1_date=.
		then do;
			n_N44unfav1_date=i;
		end;
	end;

	n_N44unfav2_date=.;
	do i = 2 to 360;
		if  n_N44unfav1_date>. and n_N44unfav1_date<i and 
			pstat1(i)='N44' and pstat1(i-1)^='N44' and n_N44unfav2_date=.
		then do;
			n_N44unfav2_date=i;
		end;
	end;

	n_N44unfav3_date=.;
	do i = 2 to 360;
		if  n_N44unfav1_date>. and n_N44unfav1_date<i and 
			n_N44unfav2_date>. and n_N44unfav2_date<i and 
			pstat1(i)='N44' and pstat1(i-1)^='N44' and n_N44unfav3_date=.
		then do;
			n_N44unfav3_date=i;
		end;
	end;

	n_N44unfav4_date=.;
	do i = 2 to 360;
		if  n_N44unfav1_date>. and n_N44unfav1_date<i and 
			n_N44unfav2_date>. and n_N44unfav2_date<i and 
			n_N44unfav3_date>. and n_N44unfav3_date<i and 
			pstat1(i)='N44' and pstat1(i-1)^='N44' and n_N44unfav4_date=.
		then do;
			n_N44unfav4_date=i;
		end;
	end;

	n_N44unfav1_date = (n_N44unfav1_date/12) + 1983;
	n_N44unfav2_date = (n_N44unfav2_date/12) + 1983;
	n_N44unfav3_date = (n_N44unfav3_date/12) + 1983;
	n_N44unfav4_date = (n_N44unfav4_date/12) + 1983;

	label n_N44unfav1_date='Date of adverse N44 (lifetime)';
	label n_N44unfav2_date='Date of adverse N44 (lifetime)';
	label n_N44unfav3_date='Date of adverse N44 (lifetime)';
	label n_N44unfav4_date='Date of adverse N44 (lifetime)';

	%macro n44(date1,date2,year);
		if 
			(n_N44unfav1_date>&date1. and n_N44unfav1_date<=&date2.)
		or
			(n_N44unfav2_date>&date1. and n_N44unfav2_date<=&date2.)
		or
			(n_N44unfav3_date>&date1. and n_N44unfav3_date<=&date2.)
		or
			(n_N44unfav4_date>&date1. and n_N44unfav4_date<=&date2.)
		then 
			n_N44unfavyear_&year.=1;
		else
			n_N44unfavyear_&year.=0;
	%mend n44;

	%n44(1990.75,1991.75,1991);
	%n44(1991.75,1992.75,1992);
	%n44(1992.75,1993.75,1993);
	%n44(1993.75,1994.75,1994);
	%n44(1994.75,1995.75,1995);
	%n44(1995.75,1996.75,1996);
	%n44(1996.75,1997.75,1997);
	%n44(1997.75,1998.75,1998);
	%n44(1998.75,1999.75,1999);
	%n44(1999.75,2000.75,2000);
	%n44(2000.75,2001.75,2001);
	%n44(2001.75,2002.75,2002);
	%n44(2002.75,2003.75,2003);
	%n44(2003.75,2004.75,2004);
	%n44(2004.75,2005.75,2005);
	%n44(2005.75,2006.75,2006);
	%n44(2006.75,2007.75,2007);
	%n44(2007.75,2008.75,2008);
	%n44(2008.75,2009.75,2009);
	%n44(2009.75,2010.75,2010);
	%n44(2010.75,2011.75,2011);
	%n44(2011.75,2012.75,2012);

* S06 ();
	n_S06unfav1_date=.;
	do i = 2 to 360;
		if  pstat1(i)='S06' and pstat1(i-1)^='S06' and n_S06unfav1_date=.
		then do;
			n_S06unfav1_date=i;
		end;
	end;

	n_S06unfav2_date=.;
	do i = 2 to 360;
		if  n_S06unfav1_date>. and n_S06unfav1_date<i and 
			pstat1(i)='S06' and pstat1(i-1)^='S06' and n_S06unfav2_date=.
		then do;
			n_S06unfav2_date=i;
		end;
	end;

	n_S06unfav3_date=.;
	do i = 2 to 360;
		if  n_S06unfav1_date>. and n_S06unfav1_date<i and 
			n_S06unfav2_date>. and n_S06unfav2_date<i and 
			pstat1(i)='S06' and pstat1(i-1)^='S06' and n_S06unfav3_date=.
		then do;
			n_S06unfav3_date=i;
		end;
	end;

	n_S06unfav4_date=.;
	do i = 2 to 360;
		if  n_S06unfav1_date>. and n_S06unfav1_date<i and 
			n_S06unfav2_date>. and n_S06unfav2_date<i and 
			n_S06unfav3_date>. and n_S06unfav3_date<i and 
			pstat1(i)='S06' and pstat1(i-1)^='S06' and n_S06unfav4_date=.
		then do;
			n_S06unfav4_date=i;
		end;
	end;

	n_S06unfav1_date = (n_S06unfav1_date/12) + 1983;
	n_S06unfav2_date = (n_S06unfav2_date/12) + 1983;
	n_S06unfav3_date = (n_S06unfav3_date/12) + 1983;
	n_S06unfav4_date = (n_S06unfav4_date/12) + 1983;

	label n_S06unfav1_date='Date of adverse S06 (lifetime)';
	label n_S06unfav2_date='Date of adverse S06 (lifetime)';
	label n_S06unfav3_date='Date of adverse S06 (lifetime)';
	label n_S06unfav4_date='Date of adverse S06 (lifetime)';

	%macro s06(date1,date2,year);
		if 
			(n_S06unfav1_date>&date1. and n_S06unfav1_date<=&date2.)
		or
			(n_S06unfav2_date>&date1. and n_S06unfav2_date<=&date2.)
		or
			(n_S06unfav3_date>&date1. and n_S06unfav3_date<=&date2.)
		or
			(n_S06unfav4_date>&date1. and n_S06unfav4_date<=&date2.)
		then 
			n_S06unfavyear_&year.=1;
		else
			n_S06unfavyear_&year.=0;
	%mend s06;

	%s06(1990.75,1991.75,1991);
	%s06(1991.75,1992.75,1992);
	%s06(1992.75,1993.75,1993);
	%s06(1993.75,1994.75,1994);
	%s06(1994.75,1995.75,1995);
	%s06(1995.75,1996.75,1996);
	%s06(1996.75,1997.75,1997);
	%s06(1997.75,1998.75,1998);
	%s06(1998.75,1999.75,1999);
	%s06(1999.75,2000.75,2000);
	%s06(2000.75,2001.75,2001);
	%s06(2001.75,2002.75,2002);
	%s06(2002.75,2003.75,2003);
	%s06(2003.75,2004.75,2004);
	%s06(2004.75,2005.75,2005);
	%s06(2005.75,2006.75,2006);
	%s06(2006.75,2007.75,2007);
	%s06(2007.75,2008.75,2008);
	%s06(2008.75,2009.75,2009);
	%s06(2009.75,2010.75,2010);
	%s06(2010.75,2011.75,2011);
	%s06(2011.75,2012.75,2012);


	* M02 ();
	n_M02unfav1_date=.;
	do i = 2 to 360;
		if  pstat1(i)='M02' and pstat1(i-1)^='M02' and n_M02unfav1_date=.
		then do;
			n_M02unfav1_date=i;
		end;
	end;

	n_M02unfav2_date=.;
	do i = 2 to 360;
		if  n_M02unfav1_date>. and n_M02unfav1_date<i and 
			pstat1(i)='M02' and pstat1(i-1)^='M02' and n_M02unfav2_date=.
		then do;
			n_M02unfav2_date=i;
		end;
	end;

	n_M02unfav3_date=.;
	do i = 2 to 360;
		if  n_M02unfav1_date>. and n_M02unfav1_date<i and 
			n_M02unfav2_date>. and n_M02unfav2_date<i and 
			pstat1(i)='M02' and pstat1(i-1)^='M02' and n_M02unfav3_date=.
		then do;
			n_M02unfav3_date=i;
		end;
	end;

	n_M02unfav4_date=.;
	do i = 2 to 360;
		if  n_M02unfav1_date>. and n_M02unfav1_date<i and 
			n_M02unfav2_date>. and n_M02unfav2_date<i and 
			n_M02unfav3_date>. and n_M02unfav3_date<i and 
			pstat1(i)='M02' and pstat1(i-1)^='M02' and n_M02unfav4_date=.
		then do;
			n_M02unfav4_date=i;
		end;
	end;

	n_M02unfav1_date = (n_M02unfav1_date/12) + 1983;
	n_M02unfav2_date = (n_M02unfav2_date/12) + 1983;
	n_M02unfav3_date = (n_M02unfav3_date/12) + 1983;
	n_M02unfav4_date = (n_M02unfav4_date/12) + 1983;

	label n_M02unfav1_date='Date of adverse M02 (lifetime)';
	label n_M02unfav2_date='Date of adverse M02 (lifetime)';
	label n_M02unfav3_date='Date of adverse M02 (lifetime)';
	label n_M02unfav4_date='Date of adverse M02 (lifetime)';

	%macro m02(date1,date2,year);
		if 
			(n_M02unfav1_date>&date1. and n_M02unfav1_date<=&date2.)
		or
			(n_M02unfav2_date>&date1. and n_M02unfav2_date<=&date2.)
		or
			(n_M02unfav3_date>&date1. and n_M02unfav3_date<=&date2.)
		or
			(n_M02unfav4_date>&date1. and n_M02unfav4_date<=&date2.)
		then 
			n_M02unfavyear_&year.=1;
		else
			n_M02unfavyear_&year.=0;
	%mend m02;

	%m02(1990.75,1991.75,1991);
	%m02(1991.75,1992.75,1992);
	%m02(1992.75,1993.75,1993);
	%m02(1993.75,1994.75,1994);
	%m02(1994.75,1995.75,1995);
	%m02(1995.75,1996.75,1996);
	%m02(1996.75,1997.75,1997);
	%m02(1997.75,1998.75,1998);
	%m02(1998.75,1999.75,1999);
	%m02(1999.75,2000.75,2000);
	%m02(2000.75,2001.75,2001);
	%m02(2001.75,2002.75,2002);
	%m02(2002.75,2003.75,2003);
	%m02(2003.75,2004.75,2004);
	%m02(2004.75,2005.75,2005);
	%m02(2005.75,2006.75,2006);
	%m02(2006.75,2007.75,2007);
	%m02(2007.75,2008.75,2008);
	%m02(2008.75,2009.75,2009);
	%m02(2009.75,2010.75,2010);
	%m02(2010.75,2011.75,2011);
	%m02(2011.75,2012.75,2012);

* CREATE DUMMIES FOR TERMINATION REASONS;

	* Get payment status codes in the 12 months before termination (and termination date);
	do i=2 to 360;
		if 
			(pstat1(i)='T01' or pstat1(i)='T20' or pstat1(i)='T22'
			or pstat1(i)='T30' or pstat1(i)='T31' or pstat1(i)='T32'
			or pstat1(i)='T33' or pstat1(i)='T50' or pstat1(i)='T51')
		and
			(pstat1(i-1)^='T01' and pstat1(i-1)^='T20' and pstat1(i-1)^='T22'
			and pstat1(i-1)^='T30' and pstat1(i-1)^='T31' and pstat1(i-1)^='T32'
			and pstat1(i-1)^='T33' and pstat1(i-1)^='T50' and pstat1(i-1)^='T51')
		then do;
			n_termdate=i;
			if i>1 then n_termreas1=pstat1(i-1);
			if i>2 then n_termreas2=pstat1(i-2);
			if i>3 then n_termreas3=pstat1(i-3);
			if i>4 then n_termreas4=pstat1(i-4);
			if i>5 then n_termreas5=pstat1(i-5);
			if i>6 then n_termreas6=pstat1(i-6);
			if i>7 then n_termreas7=pstat1(i-7);
			if i>8 then n_termreas8=pstat1(i-8);
			if i>9 then n_termreas9=pstat1(i-9);
			if i>10 then n_termreas10=pstat1(i-10);
			if i>11 then n_termreas11=pstat1(i-11);
			if i>12 then n_termreas12=pstat1(i-12);
			end;
	end;
	
	* Compute n_termination year;
	n_termdate=(n_termdate/12)+1983;

	* N01: Excess income;
	if 
		n_termreas1='N01' or n_termreas2='N01' or n_termreas3='N01' or
		n_termreas4='N01' or n_termreas5='N01' or n_termreas6='N01' or
		n_termreas7='N01' or n_termreas8='N01' or n_termreas9='N01' or
		n_termreas10='N01' or n_termreas11='N01' or n_termreas12='N01'
	then n_term_N01=1;
	else n_term_N01=0;

	* N02: Public institution;
	if 
		n_termreas1='N02' or n_termreas2='N02' or n_termreas3='N02' or
		n_termreas4='N02' or n_termreas5='N02' or n_termreas6='N02' or
		n_termreas7='N02' or n_termreas8='N02' or n_termreas9='N02' or
		n_termreas10='N02' or n_termreas11='N02' or n_termreas12='N02'
	then n_term_N02=1;
	else n_term_N02=0;

	* N03: Absent from US;
	if 
		n_termreas1='N03' or n_termreas2='N03' or n_termreas3='N03' or
		n_termreas4='N03' or n_termreas5='N03' or n_termreas6='N03' or
		n_termreas7='N03' or n_termreas8='N03' or n_termreas9='N03' or
		n_termreas10='N03' or n_termreas11='N03' or n_termreas12='N03'
	then n_term_N03=1;
	else n_term_N03=0;

	* N04: Excess assets;
	if 
		n_termreas1='N04' or n_termreas2='N04' or n_termreas3='N04' or
		n_termreas4='N04' or n_termreas5='N04' or n_termreas6='N04' or
		n_termreas7='N04' or n_termreas8='N04' or n_termreas9='N04' or
		n_termreas10='N04' or n_termreas11='N04' or n_termreas12='N04'
	then n_term_N04=1;
	else n_term_N04=0;

	* N05: Unable to determine eligibility;
	if 
		n_termreas1='N05' or n_termreas2='N05' or n_termreas3='N05' or
		n_termreas4='N05' or n_termreas5='N05' or n_termreas6='N05' or
		n_termreas7='N05' or n_termreas8='N05' or n_termreas9='N05' or
		n_termreas10='N05' or n_termreas11='N05' or n_termreas12='N05'
	then n_term_N05=1;
	else n_term_N05=0;

	* N06: Refuses to file for other benefits;
	if 
		n_termreas1='N06' or n_termreas2='N06' or n_termreas3='N06' or
		n_termreas4='N06' or n_termreas5='N06' or n_termreas6='N06' or
		n_termreas7='N06' or n_termreas8='N06' or n_termreas9='N06' or
		n_termreas10='N06' or n_termreas11='N06' or n_termreas12='N06'
	then n_term_N06=1;
	else n_term_N06=0;

	* N07: Cessation of disability (CDR);
	if 
		n_termreas1='N07' or n_termreas2='N07' or n_termreas3='N07' or
		n_termreas4='N07' or n_termreas5='N07' or n_termreas6='N07' or
		n_termreas7='N07' or n_termreas8='N07' or n_termreas9='N07' or
		n_termreas10='N07' or n_termreas11='N07' or n_termreas12='N07'
	then n_term_N07=1;
	else n_term_N07=0;

	* N08: Cessation of blindness (CDR);
	if 
		n_termreas1='N08' or n_termreas2='N08' or n_termreas3='N08' or
		n_termreas4='N08' or n_termreas5='N08' or n_termreas6='N08' or
		n_termreas7='N08' or n_termreas8='N08' or n_termreas9='N08' or
		n_termreas10='N08' or n_termreas11='N08' or n_termreas12='N08'
	then n_term_N08=1;
	else n_term_N08=0;

	* N09: Refuses voc rehab;
	if 
		n_termreas1='N09' or n_termreas2='N09' or n_termreas3='N09' or
		n_termreas4='N09' or n_termreas5='N09' or n_termreas6='N09' or
		n_termreas7='N09' or n_termreas8='N09' or n_termreas9='N09' or
		n_termreas10='N09' or n_termreas11='N09' or n_termreas12='N09'
	then n_term_N09=1;
	else n_term_N09=0;

	* N10: Fails to comply with drug and alcohol treatment;
	if 
		n_termreas1='N10' or n_termreas2='N10' or n_termreas3='N10' or
		n_termreas4='N10' or n_termreas5='N10' or n_termreas6='N10' or
		n_termreas7='N10' or n_termreas8='N10' or n_termreas9='N10' or
		n_termreas10='N10' or n_termreas11='N10' or n_termreas12='N10'
	then n_term_N10=1;
	else n_term_N10=0;

	* N11: Fails to comply with treatment;
	if 
		n_termreas1='N11' or n_termreas2='N11' or n_termreas3='N11' or
		n_termreas4='N11' or n_termreas5='N11' or n_termreas6='N11' or
		n_termreas7='N11' or n_termreas8='N11' or n_termreas9='N11' or
		n_termreas10='N11' or n_termreas11='N11' or n_termreas12='N11'
	then n_term_N11=1;
	else n_term_N11=0;

	* N12: Voluntary withdrawal;
	if 
		n_termreas1='N12' or n_termreas2='N12' or n_termreas3='N12' or
		n_termreas4='N12' or n_termreas5='N12' or n_termreas6='N12' or
		n_termreas7='N12' or n_termreas8='N12' or n_termreas9='N12' or
		n_termreas10='N12' or n_termreas11='N12' or n_termreas12='N12'
	then n_term_N12=1;
	else n_term_N12=0;

	* N13: Ineligible based on citizenship status;
	if 
		n_termreas1='N13' or n_termreas2='N13' or n_termreas3='N13' or
		n_termreas4='N13' or n_termreas5='N13' or n_termreas6='N13' or
		n_termreas7='N13' or n_termreas8='N13' or n_termreas9='N13' or
		n_termreas10='N13' or n_termreas11='N13' or n_termreas12='N13'
	then n_term_N13=1;
	else n_term_N13=0;

	* N14: Not aged, blind, or disabled;
	if 
		n_termreas1='N14' or n_termreas2='N14' or n_termreas3='N14' or
		n_termreas4='N14' or n_termreas5='N14' or n_termreas6='N14' or
		n_termreas7='N14' or n_termreas8='N14' or n_termreas9='N14' or
		n_termreas10='N14' or n_termreas11='N14' or n_termreas12='N14'
	then n_term_N14=1;
	else n_term_N14=0;

	* N15: Applicant not blind;
	if 
		n_termreas1='N15' or n_termreas2='N15' or n_termreas3='N15' or
		n_termreas4='N15' or n_termreas5='N15' or n_termreas6='N15' or
		n_termreas7='N15' or n_termreas8='N15' or n_termreas9='N15' or
		n_termreas10='N15' or n_termreas11='N15' or n_termreas12='N15'
	then n_term_N15=1;
	else n_term_N15=0;

	* N16: Applicant not disabled;
	if 
		n_termreas1='N16' or n_termreas2='N16' or n_termreas3='N16' or
		n_termreas4='N16' or n_termreas5='N16' or n_termreas6='N16' or
		n_termreas7='N16' or n_termreas8='N16' or n_termreas9='N16' or
		n_termreas10='N16' or n_termreas11='N16' or n_termreas12='N16'
	then n_term_N16=1;
	else n_term_N16=0;

	* N17: Failure by applicant to pursue claim;
	if 
		n_termreas1='N17' or n_termreas2='N17' or n_termreas3='N17' or
		n_termreas4='N17' or n_termreas5='N17' or n_termreas6='N17' or
		n_termreas7='N17' or n_termreas8='N17' or n_termreas9='N17' or
		n_termreas10='N17' or n_termreas11='N17' or n_termreas12='N17'
	then n_term_N17=1;
	else n_term_N17=0;

	* N18: Failure by applicant to cooperate on claim, or can't locate;
	if 
		n_termreas1='N18' or n_termreas2='N18' or n_termreas3='N18' or
		n_termreas4='N18' or n_termreas5='N18' or n_termreas6='N18' or
		n_termreas7='N18' or n_termreas8='N18' or n_termreas9='N18' or
		n_termreas10='N18' or n_termreas11='N18' or n_termreas12='N18'
	then n_term_N18=1;
	else n_term_N18=0;

	* N19: Voluntary termination of participation;
	if 
		n_termreas1='N19' or n_termreas2='N19' or n_termreas3='N19' or
		n_termreas4='N19' or n_termreas5='N19' or n_termreas6='N19' or
		n_termreas7='N19' or n_termreas8='N19' or n_termreas9='N19' or
		n_termreas10='N19' or n_termreas11='N19' or n_termreas12='N19'
	then n_term_N19=1;
	else n_term_N19=0;

	* N20: Fails to provide information;
	if 
		n_termreas1='N20' or n_termreas2='N20' or n_termreas3='N20' or
		n_termreas4='N20' or n_termreas5='N20' or n_termreas6='N20' or
		n_termreas7='N20' or n_termreas8='N20' or n_termreas9='N20' or
		n_termreas10='N20' or n_termreas11='N20' or n_termreas12='N20'
	then n_term_N20=1;
	else n_term_N20=0;

	* N22: Inmate of penal institution;
	if 
		n_termreas1='N22' or n_termreas2='N22' or n_termreas3='N22' or
		n_termreas4='N22' or n_termreas5='N22' or n_termreas6='N22' or
		n_termreas7='N22' or n_termreas8='N22' or n_termreas9='N22' or
		n_termreas10='N22' or n_termreas11='N22' or n_termreas12='N22'
	then n_term_N22=1;
	else n_term_N22=0;

	* N23: Non-resident of US;
	if 
		n_termreas1='N23' or n_termreas2='N23' or n_termreas3='N23' or
		n_termreas4='N23' or n_termreas5='N23' or n_termreas6='N23' or
		n_termreas7='N23' or n_termreas8='N23' or n_termreas9='N23' or
		n_termreas10='N23' or n_termreas11='N23' or n_termreas12='N23'
	then n_term_N23=1;
	else n_term_N23=0;

	* N24: False or misleading statements;
	if 
		n_termreas1='N24' or n_termreas2='N24' or n_termreas3='N24' or
		n_termreas4='N24' or n_termreas5='N24' or n_termreas6='N24' or
		n_termreas7='N24' or n_termreas8='N24' or n_termreas9='N24' or
		n_termreas10='N24' or n_termreas11='N24' or n_termreas12='N24'
	then n_term_N24=1;
	else n_term_N24=0;

	* N25: Fugitive on the run;
	if 
		n_termreas1='N25' or n_termreas2='N25' or n_termreas3='N25' or
		n_termreas4='N25' or n_termreas5='N25' or n_termreas6='N25' or
		n_termreas7='N25' or n_termreas8='N25' or n_termreas9='N25' or
		n_termreas10='N25' or n_termreas11='N25' or n_termreas12='N25'
	then n_term_N25=1;
	else n_term_N25=0;

	* N27: Substantial Gainful Activity;
	if 
		n_termreas1='N27' or n_termreas2='N27' or n_termreas3='N27' or
		n_termreas4='N27' or n_termreas5='N27' or n_termreas6='N27' or
		n_termreas7='N27' or n_termreas8='N27' or n_termreas9='N27' or
		n_termreas10='N27' or n_termreas11='N27' or n_termreas12='N27'
	then n_term_N27=1;
	else n_term_N27=0;

	* N30: Slight impairment, only medical;
	if 
		n_termreas1='N30' or n_termreas2='N30' or n_termreas3='N30' or
		n_termreas4='N30' or n_termreas5='N30' or n_termreas6='N30' or
		n_termreas7='N30' or n_termreas8='N30' or n_termreas9='N30' or
		n_termreas10='N30' or n_termreas11='N30' or n_termreas12='N30'
	then n_term_N30=1;
	else n_term_N30=0;

	* N31: Capacity for SGA, customary past work;
	if 
		n_termreas1='N31' or n_termreas2='N31' or n_termreas3='N31' or
		n_termreas4='N31' or n_termreas5='N31' or n_termreas6='N31' or
		n_termreas7='N31' or n_termreas8='N31' or n_termreas9='N31' or
		n_termreas10='N31' or n_termreas11='N31' or n_termreas12='N31'
	then n_term_N31=1;
	else n_term_N31=0;

	* N32: Capacity for SGA, other than past work;
	if 
		n_termreas1='N32' or n_termreas2='N32' or n_termreas3='N32' or
		n_termreas4='N32' or n_termreas5='N32' or n_termreas6='N32' or
		n_termreas7='N32' or n_termreas8='N32' or n_termreas9='N32' or
		n_termreas10='N32' or n_termreas11='N32' or n_termreas12='N32'
	then n_term_N32=1;
	else n_term_N32=0;

	* N33: Engaging in SGA despite impairment;
	if 
		n_termreas1='N33' or n_termreas2='N33' or n_termreas3='N33' or
		n_termreas4='N33' or n_termreas5='N33' or n_termreas6='N33' or
		n_termreas7='N33' or n_termreas8='N33' or n_termreas9='N33' or
		n_termreas10='N33' or n_termreas11='N33' or n_termreas12='N33'
	then n_term_N33=1;
	else n_term_N33=0;

	* N34: Impairment prevented SGA for 12 months;
	if 
		n_termreas1='N34' or n_termreas2='N34' or n_termreas3='N34' or
		n_termreas4='N34' or n_termreas5='N34' or n_termreas6='N34' or
		n_termreas7='N34' or n_termreas8='N34' or n_termreas9='N34' or
		n_termreas10='N34' or n_termreas11='N34' or n_termreas12='N34'
	then n_term_N34=1;
	else n_term_N34=0;

	* N35: Severe impairment but not expected to last;
	if 
		n_termreas1='N35' or n_termreas2='N35' or n_termreas3='N35' or
		n_termreas4='N35' or n_termreas5='N35' or n_termreas6='N35' or
		n_termreas7='N35' or n_termreas8='N35' or n_termreas9='N35' or
		n_termreas10='N35' or n_termreas11='N35' or n_termreas12='N35'
	then n_term_N35=1;
	else n_term_N35=0;

	* N36: Insufficient or no medical data;
	if 
		n_termreas1='N36' or n_termreas2='N36' or n_termreas3='N36' or
		n_termreas4='N36' or n_termreas5='N36' or n_termreas6='N36' or
		n_termreas7='N36' or n_termreas8='N36' or n_termreas9='N36' or
		n_termreas10='N36' or n_termreas11='N36' or n_termreas12='N36'
	then n_term_N36=1;
	else n_term_N36=0;

	* N37: Failure or refusal to submit to consultative exam (CDR);
	if 
		n_termreas1='N37' or n_termreas2='N37' or n_termreas3='N37' or
		n_termreas4='N37' or n_termreas5='N37' or n_termreas6='N37' or
		n_termreas7='N37' or n_termreas8='N37' or n_termreas9='N37' or
		n_termreas10='N37' or n_termreas11='N37' or n_termreas12='N37'
	then n_term_N37=1;
	else n_term_N37=0;

	* N38: Does not want to develop claim;
	if 
		n_termreas1='N38' or n_termreas2='N38' or n_termreas3='N38' or
		n_termreas4='N38' or n_termreas5='N38' or n_termreas6='N38' or
		n_termreas7='N38' or n_termreas8='N38' or n_termreas9='N38' or
		n_termreas10='N38' or n_termreas11='N38' or n_termreas12='N38'
	then n_term_N38=1;
	else n_term_N38=0;

	* N39: Fails to follow prescribed treatment;
	if 
		n_termreas1='N39' or n_termreas2='N39' or n_termreas3='N39' or
		n_termreas4='N39' or n_termreas5='N39' or n_termreas6='N39' or
		n_termreas7='N39' or n_termreas8='N39' or n_termreas9='N39' or
		n_termreas10='N39' or n_termreas11='N39' or n_termreas12='N39'
	then n_term_N39=1;
	else n_term_N39=0;

	* N40: IFA shows impairment not of comparable severity;
	if 
		n_termreas1='N40' or n_termreas2='N40' or n_termreas3='N40' or
		n_termreas4='N40' or n_termreas5='N40' or n_termreas6='N40' or
		n_termreas7='N40' or n_termreas8='N40' or n_termreas9='N40' or
		n_termreas10='N40' or n_termreas11='N40' or n_termreas12='N40'
	then n_term_N40=1;
	else n_term_N40=0;

	* N41: Impairment not severe (visual);
	if 
		n_termreas1='N41' or n_termreas2='N41' or n_termreas3='N41' or
		n_termreas4='N41' or n_termreas5='N41' or n_termreas6='N41' or
		n_termreas7='N41' or n_termreas8='N41' or n_termreas9='N41' or
		n_termreas10='N41' or n_termreas11='N41' or n_termreas12='N41'
	then n_term_N41=1;
	else n_term_N41=0;

	* N42: Capacity for SGA, customary past work (visual);
	if 
		n_termreas1='N42' or n_termreas2='N42' or n_termreas3='N42' or
		n_termreas4='N42' or n_termreas5='N42' or n_termreas6='N42' or
		n_termreas7='N42' or n_termreas8='N42' or n_termreas9='N42' or
		n_termreas10='N42' or n_termreas11='N42' or n_termreas12='N42'
	then n_term_N42=1;
	else n_term_N42=0;

	* N43: Capacity for SGA, other work (visual);
	if 
		n_termreas1='N43' or n_termreas2='N43' or n_termreas3='N43' or
		n_termreas4='N43' or n_termreas5='N43' or n_termreas6='N43' or
		n_termreas7='N43' or n_termreas8='N43' or n_termreas9='N43' or
		n_termreas10='N43' or n_termreas11='N43' or n_termreas12='N43'
	then n_term_N43=1;
	else n_term_N43=0;

	* N44: Impairment not severe;
	if 
		n_termreas1='N44' or n_termreas2='N44' or n_termreas3='N44' or
		n_termreas4='N44' or n_termreas5='N44' or n_termreas6='N44' or
		n_termreas7='N44' or n_termreas8='N44' or n_termreas9='N44' or
		n_termreas10='N44' or n_termreas11='N44' or n_termreas12='N44'
	then n_term_N44=1;
	else n_term_N44=0;

	* N45: Impairment prevented SGA for less than 12 months (visual);
	if 
		n_termreas1='N45' or n_termreas2='N45' or n_termreas3='N45' or
		n_termreas4='N45' or n_termreas5='N45' or n_termreas6='N45' or
		n_termreas7='N45' or n_termreas8='N45' or n_termreas9='N45' or
		n_termreas10='N45' or n_termreas11='N45' or n_termreas12='N45'
	then n_term_N45=1;
	else n_term_N45=0;

	* N46: Impairment prevented SGA at adjudication but not expected to last (visual);
	if 
		n_termreas1='N46' or n_termreas2='N46' or n_termreas3='N46' or
		n_termreas4='N46' or n_termreas5='N46' or n_termreas6='N46' or
		n_termreas7='N46' or n_termreas8='N46' or n_termreas9='N46' or
		n_termreas10='N46' or n_termreas11='N46' or n_termreas12='N46'
	then n_term_N46=1;
	else n_term_N46=0;

	* N47: Insufficient or no medical evidence (visual);
	if 
		n_termreas1='N47' or n_termreas2='N47' or n_termreas3='N47' or
		n_termreas4='N47' or n_termreas5='N47' or n_termreas6='N47' or
		n_termreas7='N47' or n_termreas8='N47' or n_termreas9='N47' or
		n_termreas10='N47' or n_termreas11='N47' or n_termreas12='N47'
	then n_term_N47=1;
	else n_term_N47=0;

	* N48: Failure or refusal of consultative exam (visual);
	if 
		n_termreas1='N48' or n_termreas2='N48' or n_termreas3='N48' or
		n_termreas4='N48' or n_termreas5='N48' or n_termreas6='N48' or
		n_termreas7='N48' or n_termreas8='N48' or n_termreas9='N48' or
		n_termreas10='N48' or n_termreas11='N48' or n_termreas12='N48'
	then n_term_N48=1;
	else n_term_N48=0;

	* N49: Applicant does not want to develop claim (visual) ;
	if 
		n_termreas1='N49' or n_termreas2='N49' or n_termreas3='N49' or
		n_termreas4='N49' or n_termreas5='N49' or n_termreas6='N49' or
		n_termreas7='N49' or n_termreas8='N49' or n_termreas9='N49' or
		n_termreas10='N49' or n_termreas11='N49' or n_termreas12='N49'
	then n_term_N49=1;
	else n_term_N49=0;

	* N50: Fails to follow prescribed treatment (visual);
	if 
		n_termreas1='N50' or n_termreas2='N50' or n_termreas3='N50' or
		n_termreas4='N50' or n_termreas5='N50' or n_termreas6='N50' or
		n_termreas7='N50' or n_termreas8='N50' or n_termreas9='N50' or
		n_termreas10='N50' or n_termreas11='N50' or n_termreas12='N50'
	then n_term_N50=1;
	else n_term_N50=0;

	* N51: IFA shows not comparable severity (visual);
	if 
		n_termreas1='N51' or n_termreas2='N51' or n_termreas3='N51' or
		n_termreas4='N51' or n_termreas5='N51' or n_termreas6='N51' or
		n_termreas7='N51' or n_termreas8='N51' or n_termreas9='N51' or
		n_termreas10='N51' or n_termreas11='N51' or n_termreas12='N51'
	then n_term_N51=1;
	else n_term_N51=0;

	* N52: Deleted from state rolls prior to 12/73;
	if 
		n_termreas1='N52' or n_termreas2='N52' or n_termreas3='N52' or
		n_termreas4='N52' or n_termreas5='N52' or n_termreas6='N52' or
		n_termreas7='N52' or n_termreas8='N52' or n_termreas9='N52' or
		n_termreas10='N52' or n_termreas11='N52' or n_termreas12='N52'
	then n_term_N52=1;
	else n_term_N52=0;

	* N53: Deleted from state rolls after 12/73;
	if 
		n_termreas1='N53' or n_termreas2='N53' or n_termreas3='N53' or
		n_termreas4='N53' or n_termreas5='N53' or n_termreas6='N53' or
		n_termreas7='N53' or n_termreas8='N53' or n_termreas9='N53' or
		n_termreas10='N53' or n_termreas11='N53' or n_termreas12='N53'
	then n_term_N53=1;
	else n_term_N53=0;

	* N54: Unable to locate;
	if 
		n_termreas1='N54' or n_termreas2='N54' or n_termreas3='N54' or
		n_termreas4='N54' or n_termreas5='N54' or n_termreas6='N54' or
		n_termreas7='N54' or n_termreas8='N54' or n_termreas9='N54' or
		n_termreas10='N54' or n_termreas11='N54' or n_termreas12='N54'
	then n_term_N54=1;
	else n_term_N54=0;

	* P01: Possible reinstatement pending SGA;
	if 
		n_termreas1='P01' or n_termreas2='P01' or n_termreas3='P01' or
		n_termreas4='P01' or n_termreas5='P01' or n_termreas6='P01' or
		n_termreas7='P01' or n_termreas8='P01' or n_termreas9='P01' or
		n_termreas10='P01' or n_termreas11='P01' or n_termreas12='P01'
	then n_term_P01=1;
	else n_term_P01=0;

	* S01: Report of death;
	if 
		n_termreas1='S01' or n_termreas2='S01' or n_termreas3='S01' or
		n_termreas4='S01' or n_termreas5='S01' or n_termreas6='S01' or
		n_termreas7='S01' or n_termreas8='S01' or n_termreas9='S01' or
		n_termreas10='S01' or n_termreas11='S01' or n_termreas12='S01'
	then n_term_S01=1;
	else n_term_S01=0;

	* S04: System is awaiting disability determination;
	if 
		n_termreas1='S04' or n_termreas2='S04' or n_termreas3='S04' or
		n_termreas4='S04' or n_termreas5='S04' or n_termreas6='S04' or
		n_termreas7='S04' or n_termreas8='S04' or n_termreas9='S04' or
		n_termreas10='S04' or n_termreas11='S04' or n_termreas12='S04'
	then n_term_S04=1;
	else n_term_S04=0;

	* S05: System unable to determine prerequisite month;
	if 
		n_termreas1='S05' or n_termreas2='S05' or n_termreas3='S05' or
		n_termreas4='S05' or n_termreas5='S05' or n_termreas6='S05' or
		n_termreas7='S05' or n_termreas8='S05' or n_termreas9='S05' or
		n_termreas10='S05' or n_termreas11='S05' or n_termreas12='S05'
	then n_term_S05=1;
	else n_term_S05=0;

	* S06: Recipient's address unknown;
	if 
		n_termreas1='S06' or n_termreas2='S06' or n_termreas3='S06' or
		n_termreas4='S06' or n_termreas5='S06' or n_termreas6='S06' or
		n_termreas7='S06' or n_termreas8='S06' or n_termreas9='S06' or
		n_termreas10='S06' or n_termreas11='S06' or n_termreas12='S06'
	then n_term_S06=1;
	else n_term_S06=0;

	* S07: Returned checks (for other than normal reasons);
	if 
		n_termreas1='S07' or n_termreas2='S07' or n_termreas3='S07' or
		n_termreas4='S07' or n_termreas5='S07' or n_termreas6='S07' or
		n_termreas7='S07' or n_termreas8='S07' or n_termreas9='S07' or
		n_termreas10='S07' or n_termreas11='S07' or n_termreas12='S07'
	then n_term_S07=1;
	else n_term_S07=0;

	* S08: Representative payee development pending;
	if 
		n_termreas1='S08' or n_termreas2='S08' or n_termreas3='S08' or
		n_termreas4='S08' or n_termreas5='S08' or n_termreas6='S08' or
		n_termreas7='S08' or n_termreas8='S08' or n_termreas9='S08' or
		n_termreas10='S08' or n_termreas11='S08' or n_termreas12='S08'
	then n_term_S08=1;
	else n_term_S08=0;

	* S09: Temporary institutionalization suspense;
	if 
		n_termreas1='S09' or n_termreas2='S09' or n_termreas3='S09' or
		n_termreas4='S09' or n_termreas5='S09' or n_termreas6='S09' or
		n_termreas7='S09' or n_termreas8='S09' or n_termreas9='S09' or
		n_termreas10='S09' or n_termreas11='S09' or n_termreas12='S09'
	then n_term_S09=1;
	else n_term_S09=0;

	* S10: Refuses direct deposit;
	if 
		n_termreas1='S10' or n_termreas2='S10' or n_termreas3='S10' or
		n_termreas4='S10' or n_termreas5='S10' or n_termreas6='S10' or
		n_termreas7='S10' or n_termreas8='S10' or n_termreas9='S10' or
		n_termreas10='S10' or n_termreas11='S10' or n_termreas12='S10'
	then n_term_S10=1;
	else n_term_S10=0;

	* S20: Recipient presumptively blind or disabled;
	if 
		n_termreas1='S20' or n_termreas2='S20' or n_termreas3='S20' or
		n_termreas4='S20' or n_termreas5='S20' or n_termreas6='S20' or
		n_termreas7='S20' or n_termreas8='S20' or n_termreas9='S20' or
		n_termreas10='S20' or n_termreas11='S20' or n_termreas12='S20'
	then n_term_S20=1;
	else n_term_S20=0;

	* S21: Recipient presumptively blind or disabled;
	if 
		n_termreas1='S21' or n_termreas2='S21' or n_termreas3='S21' or
		n_termreas4='S21' or n_termreas5='S21' or n_termreas6='S21' or
		n_termreas7='S21' or n_termreas8='S21' or n_termreas9='S21' or
		n_termreas10='S21' or n_termreas11='S21' or n_termreas12='S21'
	then n_term_S21=1;
	else n_term_S21=0;

	* S90: PR1 change in process;
	if 
		n_termreas1='S90' or n_termreas2='S90' or n_termreas3='S90' or
		n_termreas4='S90' or n_termreas5='S90' or n_termreas6='S90' or
		n_termreas7='S90' or n_termreas8='S90' or n_termreas9='S90' or
		n_termreas10='S90' or n_termreas11='S90' or n_termreas12='S90'
	then n_term_S90=1;
	else n_term_S90=0;

	* S91: PR1 change in process;
	if 
		n_termreas1='S91' or n_termreas2='S91' or n_termreas3='S91' or
		n_termreas4='S91' or n_termreas5='S91' or n_termreas6='S91' or
		n_termreas7='S91' or n_termreas8='S91' or n_termreas9='S91' or
		n_termreas10='S91' or n_termreas11='S91' or n_termreas12='S91'
	then n_term_S91=1;
	else n_term_S91=0;

	* H10: Living arrangements change in process;
	if 
		n_termreas1='H10' or n_termreas2='H10' or n_termreas3='H10' or
		n_termreas4='H10' or n_termreas5='H10' or n_termreas6='H10' or
		n_termreas7='H10' or n_termreas8='H10' or n_termreas9='H10' or
		n_termreas10='H10' or n_termreas11='H10' or n_termreas12='H10'
	then n_term_H10=1;
	else n_term_H10=0;

	* H20: Marital status change in process;
	if 
		n_termreas1='H20' or n_termreas2='H20' or n_termreas3='H20' or
		n_termreas4='H20' or n_termreas5='H20' or n_termreas6='H20' or
		n_termreas7='H20' or n_termreas8='H20' or n_termreas9='H20' or
		n_termreas10='H20' or n_termreas11='H20' or n_termreas12='H20'
	then n_term_H20=1;
	else n_term_H20=0;

	* H30: Resource change in process;
	if 
		n_termreas1='H30' or n_termreas2='H30' or n_termreas3='H30' or
		n_termreas4='H30' or n_termreas5='H30' or n_termreas6='H30' or
		n_termreas7='H30' or n_termreas8='H30' or n_termreas9='H30' or
		n_termreas10='H30' or n_termreas11='H30' or n_termreas12='H30'
	then n_term_H30=1;
	else n_term_H30=0;

	* H40: Student status change in process;
	if 
		n_termreas1='H40' or n_termreas2='H40' or n_termreas3='H40' or
		n_termreas4='H40' or n_termreas5='H40' or n_termreas6='H40' or
		n_termreas7='H40' or n_termreas8='H40' or n_termreas9='H40' or
		n_termreas10='H40' or n_termreas11='H40' or n_termreas12='H40'
	then n_term_H40=1;
	else n_term_H40=0;

	* H50: Head of household change in process;
	if 
		n_termreas1='H50' or n_termreas2='H50' or n_termreas3='H50' or
		n_termreas4='H50' or n_termreas5='H50' or n_termreas6='H50' or
		n_termreas7='H50' or n_termreas8='H50' or n_termreas9='H50' or
		n_termreas10='H50' or n_termreas11='H50' or n_termreas12='H50'
	then n_term_H50=1;
	else n_term_H50=0;

	* H60: Pending receipt of date of death;
	if 
		n_termreas1='H60' or n_termreas2='H60' or n_termreas3='H60' or
		n_termreas4='H60' or n_termreas5='H60' or n_termreas6='H60' or
		n_termreas7='H60' or n_termreas8='H60' or n_termreas9='H60' or
		n_termreas10='H60' or n_termreas11='H60' or n_termreas12='H60'
	then n_term_H60=1;
	else n_term_H60=0;

	* H70: Pending posting of payment made outside system;
	if 
		n_termreas1='H70' or n_termreas2='H70' or n_termreas3='H70' or
		n_termreas4='H70' or n_termreas5='H70' or n_termreas6='H70' or
		n_termreas7='H70' or n_termreas8='H70' or n_termreas9='H70' or
		n_termreas10='H70' or n_termreas11='H70' or n_termreas12='H70'
	then n_term_H70=1;
	else n_term_H70=0;

	* H80: Early input case, waiting on den_termination;
	if 
		n_termreas1='H80' or n_termreas2='H80' or n_termreas3='H80' or
		n_termreas4='H80' or n_termreas5='H80' or n_termreas6='H80' or
		n_termreas7='H80' or n_termreas8='H80' or n_termreas9='H80' or
		n_termreas10='H80' or n_termreas11='H80' or n_termreas12='H80'
	then n_term_H80=1;
	else n_term_H80=0;

	* H90: Systems limitation regardign computation;
	if 
		n_termreas1='H90' or n_termreas2='H90' or n_termreas3='H90' or
		n_termreas4='H90' or n_termreas5='H90' or n_termreas6='H90' or
		n_termreas7='H90' or n_termreas8='H90' or n_termreas9='H90' or
		n_termreas10='H90' or n_termreas11='H90' or n_termreas12='H90'
	then n_term_H90=1;
	else n_term_H90=0;

	* M01: Force payment case;
	if 
		n_termreas1='M01' or n_termreas2='M01' or n_termreas3='M01' or
		n_termreas4='M01' or n_termreas5='M01' or n_termreas6='M01' or
		n_termreas7='M01' or n_termreas8='M01' or n_termreas9='M01' or
		n_termreas10='M01' or n_termreas11='M01' or n_termreas12='M01'
	then n_term_M01=1;
	else n_term_M01=0;

	* M02: Force due case;
	if 
		n_termreas1='M02' or n_termreas2='M02' or n_termreas3='M02' or
		n_termreas4='M02' or n_termreas5='M02' or n_termreas6='M02' or
		n_termreas7='M02' or n_termreas8='M02' or n_termreas9='M02' or
		n_termreas10='M02' or n_termreas11='M02' or n_termreas12='M02'
	then n_term_M02=1;
	else n_term_M02=0;

	* Create dummy for ever terminated for any reason;
	n_rem_temp=SUM(OF n_term_:);
	if n_rem_temp>0 then n_remove=1;
		else n_remove=0;
	drop n_rem_temp;
	label n_remove='Removed from SSI in lifetime';


* CREATE DUMMIES FOR TERMINATION REASONS BEFORE AGE 18;
	n_age18_date=n_age18_yr+(n_age18_mo/12)+(n_age18_day/365);

	%macro age18term(code);
	if n_term_&code.=1 and n_termdate<n_age18_date
		then n_term18_&code.=1;
		else n_term18_&code.=0;
	%mend age18term;

	%age18term(N01); %age18term(N02); %age18term(N03); %age18term(N04);
	%age18term(N05); %age18term(N06); %age18term(N07); %age18term(N08);
	%age18term(N09); %age18term(N10); %age18term(N11); %age18term(N12);
	%age18term(N13); %age18term(N14); %age18term(N15); %age18term(N16);
	%age18term(N17); %age18term(N18); %age18term(N19); %age18term(N20);
	%age18term(N22); %age18term(N23); %age18term(N24); %age18term(N25); 
	%age18term(N27); %age18term(N30); %age18term(N31); %age18term(N32); 
	%age18term(N33); %age18term(N34); %age18term(N35); %age18term(N36); 
	%age18term(N37); %age18term(N38); %age18term(N39); %age18term(N40); 
	%age18term(N41); %age18term(N42); %age18term(N43); %age18term(N44); 
	%age18term(N45); %age18term(N46); %age18term(N47); %age18term(N48); 
	%age18term(N49); %age18term(N50); %age18term(N51); %age18term(N52); 
	%age18term(N53); %age18term(N54);

	%age18term(P01);

	%age18term(S01); %age18term(S04); %age18term(S05); %age18term(S06);
	%age18term(S07); %age18term(S08); %age18term(S09); %age18term(S10);
	%age18term(S20); %age18term(S21); %age18term(S90); %age18term(S91);

	%age18term(H10); %age18term(H20); %age18term(H30); %age18term(H40);
	%age18term(H50); %age18term(H60); %age18term(H70); %age18term(H80);
	%age18term(H90);

	%age18term(M01); %age18term(M02);

	* Create dummy for terminated before 18 for any reason;
	n_rem18_temp = SUM(OF n_term18_:);
	if n_rem18_temp>0 then n_remove18=1;
		else n_remove18=0;
	drop n_rem18_temp;
	label n_remove18='Removed from SSI before 18';

* FIND DATE OF LAST PAYMENT;
	array n_cum(0:360) 	n_cum_0
						n_cum_1983_1-n_cum_1983_12
						n_cum_1984_1-n_cum_1984_12
						n_cum_1985_1-n_cum_1985_12
						n_cum_1986_1-n_cum_1986_12
						n_cum_1987_1-n_cum_1987_12
						n_cum_1988_1-n_cum_1988_12
						n_cum_1989_1-n_cum_1989_12
						n_cum_1990_1-n_cum_1990_12
						n_cum_1991_1-n_cum_1991_12
						n_cum_1992_1-n_cum_1992_12
						n_cum_1993_1-n_cum_1993_12
						n_cum_1994_1-n_cum_1994_12
						n_cum_1995_1-n_cum_1995_12
						n_cum_1996_1-n_cum_1996_12
						n_cum_1997_1-n_cum_1997_12
						n_cum_1998_1-n_cum_1998_12
						n_cum_1999_1-n_cum_1999_12
						n_cum_2000_1-n_cum_2000_12
						n_cum_2001_1-n_cum_2001_12
						n_cum_2002_1-n_cum_2002_12
						n_cum_2003_1-n_cum_2003_12
						n_cum_2004_1-n_cum_2004_12
						n_cum_2005_1-n_cum_2005_12
						n_cum_2006_1-n_cum_2006_12
						n_cum_2007_1-n_cum_2007_12
						n_cum_2008_1-n_cum_2008_12
						n_cum_2009_1-n_cum_2009_12
						n_cum_2010_1-n_cum_2010_12
						n_cum_2011_1-n_cum_2011_12
						n_cum_2012_1-n_cum_2012_12;
	
	%array1d(fedpmt);
	%array1d(n_last);
	
	n_cum_0=0;	
	do i = 1 to 360;
		n_cum(i) = n_cum(i-1)+fedpmt1(i);
	end;

	do i = 1 to 360;
		if n_cum(i)=n_cum_2012_12 & n_cum(i-1)^=n_cum_2012_12
		then n_last1(i)=1;
			else n_last1(i)=0;
	end;
	
	drop n_cum_0;
	%drop(n_cum);

	%array2d(n_last);
	n_lastpay_yr=.;
	n_lastpay_mo=.;
	do i = 1983 to 2012;
		do j = 1 to 12;
			if n_last(i,j)=1 then n_lastpay_yr=i;
			if n_last(i,j)=1 then n_lastpay_mo=j;
		end;
	end;
	label n_lastpay_yr='Year of last payment';
	label n_lastpay_mo='Month of last payment';

	%drop(n_last);

	* Calculate age at last payment;
	n_lastage = (n_lastpay_yr - dobyy) + ((n_lastpay_mo-n_dobmm)/12);
	label n_lastage='Age at last payment';


* CALCULATE AGE WHEN RECORD WAS ESTABLISHED;
	
	* Break out year, month, day when record was established;
	n_estpd_char = put(estpd,$8.); 
	n_estpd_num = input(estpd,8.0); 
	n_estpd_yr = substr(n_estpd_char,1,4); 
	n_estpd_yr = input(n_estpd_yr,4.0); 
	label n_estpd_yr='Year record established';

	n_estpd_mo = substr(n_estpd_char,5,2); 
	n_estpd_mo = input(n_estpd_mo,2.0); 
	label n_estpd_mo='Month record established';

	n_estpd_day = substr(n_estpd_char,7,2);
	n_estpd_day = input(n_estpd_day,2.0);
	label n_estpd_day='Day record established';

	* Calculate age when record was established;
	n_estpd_age = (n_estpd_yr - dobyy) + ((n_estpd_mo-n_dobmm)/12) + 
		((n_estpd_day-n_dobdd)/365);
	label n_estpd_age='Age when record established';


* CREATE DIAGNOSIS CATEGORIES;
	%macro diag1(start,end,type);
		if (&start.<dibdig<&end.) then n_diag1_&type.=1;
		else n_diag1_&type.=0;
	%mend diag1;

	%macro diag2(start,end,type);
		if (&start.<dibdig2<&end.) then n_diag2_&type.=1;
		else n_diag2_&type.=0;
	%mend diag2;

	if (dibdig=0) then n_diag1_none=1;
		else n_diag1_none=0;
	%diag1(0,1400,infec);
	%diag1(1400,2400,neo);
	%diag1(2400,2800,endo);
	%diag1(2800,2900,blood);
	%diag1(2900,3200,mental);
	%diag1(3200,3600,nerv);
	%diag1(3600,3900,sense);
	%diag1(3900,4600,circ);
	%diag1(4600,5200,resp);
	%diag1(5200,5800,dig);
	%diag1(5800,6300,gu);
	%diag1(6300,6800,preg);
	%diag1(6800,7100,skin);
	%diag1(7100,7400,musc);
	%diag1(7400,7600,cong);
	%diag1(7600,7800,nat);
	%diag1(7800,8000,ill);
	%diag1(8000,10000,inj);

	if (dibdig2=0) then n_diag2_none=1;
		else n_diag2_none=0;
	%diag2(0,1400,infec);
	%diag2(1400,2400,neo);
	%diag2(2400,2800,endo);
	%diag2(2800,2900,blood);
	%diag2(2900,3200,mental);
	%diag2(3200,3600,nerv);
	%diag2(3600,3900,sense);
	%diag2(3900,4600,circ);
	%diag2(4600,5200,resp);
	%diag2(5200,5800,dig);
	%diag2(5800,6300,gu);
	%diag2(6300,6800,preg);
	%diag2(6800,7100,skin);
	%diag2(7100,7400,musc);
	%diag2(7400,7600,cong);
	%diag2(7600,7800,nat);
	%diag2(7800,8000,ill);
	%diag2(8000,10000,inj);


	/* CALCULATE AVERAGE NON-ZERO FEDAMT VALUES */
	%array1d(fedamt);
	%array1d(fedamtmiss);
 	do i = 1 to 360;                                              
   		if pstat1(i)='C01' then fedamtmiss1(i)=fedamt1(i);
			else fedamtmiss1(i)=.; 
  	end;

	%array2d(fedamtmiss);
	array n_payavg(1983:2012) n_payavg_1983-n_payavg_2012;
	do i = 1983 to 2012;
		n_payavg(i) = MEAN(	fedamtmiss(i,1), fedamtmiss(i,2), fedamtmiss(i,3), fedamtmiss(i,4),
							fedamtmiss(i,5), fedamtmiss(i,6), fedamtmiss(i,7), fedamtmiss(i,8),
							fedamtmiss(i,9), fedamtmiss(i,10), fedamtmiss(i,11), fedamtmiss(i,12));
	end;

	%drop(pstat); %drop(fedamt); %drop(fedpmt); %drop(statpmt); %drop(fedamtmiss);
	
run;
